package cn.tedu.straw.portal.controller;


import cn.tedu.straw.commons.model.Question;
import cn.tedu.straw.commons.model.UserCollection;

import cn.tedu.straw.commons.security.UserInfo;
import cn.tedu.straw.portal.service.IUserCollectionService;
import cn.tedu.straw.portal.service.impl.UserCollectionServiceImpl;
import cn.tedu.straw.commons.vo.R;
import cn.tedu.straw.commons.vo.UserCollectionVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import sun.rmi.runtime.Log;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-07-30
 */
@RestController
@RequestMapping("/v1/userCollection")
public class UserCollectionController {

    @Autowired
    private IUserCollectionService userCollectionService;
    @Autowired
    private UserCollectionServiceImpl userCollectionServiceImpl;

    @RequestMapping("/my")
    public R<PageInfo<UserCollectionVO>> get(Integer page, @AuthenticationPrincipal UserInfo userInfo){
        if (page==null||page<1){
            page=1;
        }
        PageInfo<UserCollectionVO> userCollectionVOPageInfo = userCollectionService.getUserCollections(userInfo.getId(), page);
        return R.ok(userCollectionVOPageInfo);
    }
    @RequestMapping("/post/{questionId}")
    public R<UserCollection> post(@PathVariable("questionId") Integer questionId, @AuthenticationPrincipal UserInfo userInfo){
        System.out.println(userInfo.getId());
        System.out.println(questionId);
        UserCollection post = userCollectionService.post(userInfo.getId(), questionId);
        return R.ok(post);
    }

    @RequestMapping("/delect/{questionId}")
    public R<UserCollection> deletes(@PathVariable("questionId") Integer questionId, @AuthenticationPrincipal UserInfo userInfo){
        Integer deletes = userCollectionService.deletes(userInfo.getId(), questionId);
        return R.ok(deletes);
    }
    @RequestMapping("/selectByIds/{questionId}")
    public R<UserCollection> selectByIds(@PathVariable("questionId") Integer questionId, @AuthenticationPrincipal UserInfo userInfo){
        Integer selectByIds = userCollectionService.selectByIds(userInfo.getId(), questionId);
        return R.ok(selectByIds);
    }

    @RequestMapping("/countCollect/{questionId}")
    public R<UserCollection> countCollect(@PathVariable("questionId") Integer questionId){
        Integer countCollect = userCollectionServiceImpl.countCollect(questionId);
        return R.ok(countCollect);
    }
}
